/*
 * ENUM Discoverer. ENUM Demonstration software, demonstrating the potential of ENUM.
 * 
 * Copyright (C) 2009 SIDN and ISOC.nl
 * 
 * This file is part of ENUM Discoverer.
 *
 * ENUM Discoverer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * ENUM Discoverer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with ENUM Discoverer.  If not, see <http://www.gnu.org/licenses/>.
 */

package nl.sidn.enumdiscoverer;

import android.os.Parcel;
import android.os.Parcelable;

/**
 * Data structure containing those elements of an ENUM DNS record that we are interested in
 * @author	Grant Patterson (grant.patterson@catalyst.net.nz)
 */
public class EnumRecord implements Parcelable, Comparable<EnumRecord> {

	private final int mOrder;
	private final int mPreference;
	private final String mService;
	private final String mUrl;
	
	public EnumRecord(int order, int preference, String service, String url) {
		
		mOrder = order;
		mPreference =  preference;
		mService = service;
		mUrl = url;
	}
	
	public int getOrder() {
		return mOrder;
	}
	
	public int getPreference() {
		return mPreference;
	}
	
	public String getService() {
		return mService;
	}
	
	public String getUrl() {
		return mUrl;
	}

	@Override
	public int compareTo(EnumRecord other) {
		
		if (this.mOrder != other.mOrder) {
			
			return this.mOrder - other.mOrder;
			
		} else { // same order. Use preference to distinguish
			
			return this.mPreference - other.mPreference;
		}
	}
	
	@Override
	public String toString() {
		return String.format("%s{%s, %s, %d, %d}", EnumRecord.class.getSimpleName(), mService, mUrl, mOrder, mPreference);
	}

	@Override
	public int describeContents() {
		// no special objects in this Parcelable
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		
		dest.writeInt(mOrder);
		dest.writeInt(mPreference);
		dest.writeString(mService);
		dest.writeString(mUrl);
	}
	
	/**
	 * Required for Parcelables
	 */
	public static final Parcelable.Creator<EnumRecord> CREATOR
			= new Parcelable.Creator<EnumRecord>() {
		public EnumRecord createFromParcel(Parcel in) {
			return new EnumRecord(in);
		}

		public EnumRecord[] newArray(int size) {
			return new EnumRecord[size];
		}
	};
	
	/**
	 * For use by CREATOR
	 * @param in
	 */
	private EnumRecord(Parcel in) {
		
		mOrder = in.readInt();
		mPreference = in.readInt();
		mService = in.readString();
		mUrl = in.readString();
	}
}
